import Types from '../actionTypes';
import {getToken} from '../../api';

const GET_LOGIN = (username, password) => {
  return new Promise((resolve, reject) => {
    getToken(username, password).then(res => {
      const token = res.data.access_token;
      const mynav = res.data.navlink;
      localStorage.setItem('mynav', JSON.stringify(mynav));
      localStorage.setItem('token', token);
      localStorage.setItem('username', username);
      resolve({
        type: Types.GET_LOGIN,
        value: token
      });
      window.location.replace('/');
    }).catch(err => {
      console.log(err);
    });
  });
};

export default {
  GET_LOGIN
};
